package game.jetclan.main;

import com.jme3.app.FlyCamAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.input.controls.AnalogListener;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.DefaultScreenController;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * Ashes To Ashes
 *
 * @author marcus.wass
 */
public class Main extends SimpleApplication {

    private BitmapText helloText;
    private BitmapText selectedText;
    private Nifty nifty;
    GameAppState gameState;
    MenuAppState menuState;
    //PlayableObject selectedGameUnit = null;
    private Geometry selectedGeometry = null;
    private static AppSettings cfg;

    static {
    }

    public static void main(String[] args) {
        Main app = new Main();

        cfg = new AppSettings(true);
        cfg.setFrameRate(60); // set to less than or equal screen refresh rate
        cfg.setVSync(true);   // prevents page tearing
        cfg.setFrequency(60); // set to screen refresh rate
        cfg.setResolution(1600, 900);
        cfg.setFullscreen(true);
        cfg.setSamples(2);    // anti-aliasing
        cfg.setTitle("Ashes to Ashes"); // branding: window name
        try {
            // Branding: window icon
            cfg.setIcons(new BufferedImage[]{ImageIO.read(new File("assets/Interface/flareCG_small_new_logo.png"))});
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Icon missing.", ex);
        }
// branding: load splashscreen from assets
        cfg.setSettingsDialogImage("Interface/hershel_green.jpg");
        app.setShowSettings(false); // or don't display splashscreen
        app.setSettings(cfg);
        app.setPauseOnLostFocus(false);
        app.setDisplayStatView(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        getStateManager().detach(getStateManager().getState(FlyCamAppState.class));
        menuState = new MenuAppState();
        gameState = new GameAppState(512,512);
        initNiftyGui();
        loadMenu();
    }

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
//        helloText.setText("X-coord: " + inputManager.getCursorPosition().getX() + " - Y-coord: " + inputManager.getCursorPosition().getY());
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }

    public void startGame() {
        stateManager.detach(menuState);
        stateManager.attach(gameState);
        nifty.gotoScreen("hudScreen");
    }

    public void quitGame() {
        this.stop(false);
    }

    public void loadMenu() {
        stateManager.detach(gameState);
        stateManager.attach(menuState);
        nifty.gotoScreen("menuScreen");
    }

    private void initNiftyGui() {

        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);
        nifty = niftyDisplay.getNifty();
        //Register controller is important before loading the XML files
        nifty.registerScreenController(menuState);
        nifty.registerScreenController(gameState);
        nifty.addXml("Interface/Screens/ashestoashes_hud.xml");
        nifty.addXml("Interface/Screens/ashestoashes_menu.xml");
        nifty.setDebugOptionPanelColors(false);
        menuState.setNifty(nifty);
        gameState.setNifty(nifty);
        guiViewPort.addProcessor(niftyDisplay);
        
        /**
         * nifty.getCurrentScreen().findElementByName(id);
         */
    }
}
